package com.control.qct.businesssteward.utils;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.ShopSelectAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.httputils.NetProtocol;
import com.control.qct.businesssteward.model.StoreModel;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.ArrayList;
import java.util.List;

/**
 * 门店选择
 * Created by Administrator on 2017/12/14 0014.
 */

public class PopShopSelectUtils {
    private ICallbackStoreListener mListener;
    private StringBuilder mStoreId;
    private StringBuilder mStoreName;
    private List<String> mStoreIdList;
    private List<String> mStoreNameList;
    private List<StoreModel> mStoreModelList = new ArrayList<>();
    private String storeId;
    private String storeName;
    private View mView;
    private PopupWindow mPop;
    private TextView mTvSure;
    private TextView mTvCancel;
    private RecyclerView mRvShopSelect;
    private ShopSelectAdapter mShopSelectAdapter;


    /**
     * 接口实例化
     *
     * @param listener
     */
    public void registerListener(ICallbackStoreListener listener, NetProtocol netProtocol, final BaseActivity
            activity, int id) {
        mListener = listener;
        mStoreId = new StringBuilder();
        mStoreName = new StringBuilder();
        mStoreIdList = new ArrayList<>();
        mStoreNameList = new ArrayList<>();
        getStore(netProtocol, activity);
        mView = View.inflate(activity, R.layout.pop_shop_select, null);
        mTvSure = mView.findViewById(R.id.tv_sure);
        mTvCancel = mView.findViewById(R.id.tv_cancel);
        mRvShopSelect = mView.findViewById(R.id.rv_shop_select);
        FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(activity);
        flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP); //设置是否换行
        flexboxLayoutManager.setFlexDirection(FlexDirection.ROW); //设置子元素的排列方向 默认水平排列
        flexboxLayoutManager.setAlignItems(AlignItems.STRETCH);//沿副轴对齐(单行起作用) 高度充满
        flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START);//沿主轴左对齐
        mShopSelectAdapter = new ShopSelectAdapter(mStoreModelList, id);
        mRvShopSelect.setLayoutManager(flexboxLayoutManager);
        mRvShopSelect.setAdapter(mShopSelectAdapter);

    }

    /**
     * 设置底部弹窗 -- 选择门店/多选
     */
    public void setBottomPop(Activity activity, int layoutId, String storeId) {
        mPop = PopUtils.getInstance().getBottomPop(activity, mView);
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPop.dismiss();
            }
        });
        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onSure(mStoreId.toString(), mStoreName.toString());
                }
                mPop.dismiss();
            }
        });
        String[] storeIdList = storeId.split(",");
        mStoreIdList.clear();
        mStoreNameList.clear();
        for (int i = 0; i < mShopSelectAdapter.getData().size(); i++) {//多选判断 是否已选择
            mShopSelectAdapter.getData().get(i).setChecked(false);
            for (int j = 0; j < storeIdList.length; j++) {
                if (mShopSelectAdapter.getData().get(i).getStoreId().equals(storeIdList[j])) {
                    mShopSelectAdapter.getData().get(i).setChecked(true);
                    mStoreIdList.add(mShopSelectAdapter.getData().get(i).getStoreId());
                    mStoreNameList.add(mShopSelectAdapter.getData().get(i).getTitle());
                }
            }
        }
        mShopSelectAdapter.notifyDataSetChanged();
        mShopSelectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ((List<StoreModel>) adapter.getData()).get(position).setChecked(((CheckBox) adapter.getViewByPosition
                        (mRvShopSelect, position, R.id.cb_shop_select)).isChecked());

                //多选--组合选择的门店id 门店名称 通过接口返回
                if (((List<StoreModel>) adapter.getData()).get(position).isChecked()) {
                    mStoreIdList.add(((List<StoreModel>) adapter.getData()).get(position).getStoreId());
                    mStoreNameList.add(((List<StoreModel>) adapter.getData()).get(position).getTitle());
                } else {
                    mStoreIdList.remove(((List<StoreModel>) adapter.getData()).get(position).getStoreId());
                    mStoreNameList.remove(((List<StoreModel>) adapter.getData()).get(position).getTitle());
                }
                mStoreId.delete(0, mStoreId.length());
                mStoreName.delete(0, mStoreName.length());
                for (int i = 0; i < mStoreIdList.size(); i++) {
                    mStoreId.append(mStoreIdList.get(i));
                    mStoreName.append(mStoreNameList.get(i));
                    if (i < mStoreIdList.size() - 1) {
                        mStoreId.append(",");
                        mStoreName.append(",");
                    }
                }
            }
        });

        //设置PopupWindow显示位置
        mPop.showAtLocation(activity.findViewById(layoutId), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    /**
     * 设置底部弹窗 -- 选择门店/单选
     */
    public void setSingleBottomPop(Activity activity, int layoutId, String id) {
        mPop = PopUtils.getInstance().getBottomPop(activity, mView);
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPop.dismiss();
            }
        });
        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onSure(storeId, storeName);
                }
                mPop.dismiss();
            }
        });
        mShopSelectAdapter.setPosition(-1);
        for (int i = 0; i < mStoreModelList.size(); i++) {
            if (mStoreModelList.get(i).getStoreId().equals(id)) {
                mStoreModelList.get(i).setChecked(true);
                mShopSelectAdapter.setPosition(i);
            }
        }
        mShopSelectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ((List<StoreModel>) adapter.getData()).get(position).setChecked(((CheckBox) adapter.getViewByPosition
                        (mRvShopSelect, position, R.id.cb_shop_select)).isChecked());
                if (((List<StoreModel>) adapter.getData()).get(position).isChecked()) {
                    storeId = ((List<StoreModel>) adapter.getData()).get(position).getStoreId();
                    storeName = ((List<StoreModel>) adapter.getData()).get(position).getTitle();
                }else {
                    storeId = "";
                    storeName = "";
                }
                mShopSelectAdapter.setPosition(position);
            }
        });

        //设置PopupWindow显示位置
        mPop.showAtLocation(activity.findViewById(layoutId), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }


    /**
     * 获取门店
     */
    private void getStore(NetProtocol netProtocol, final BaseActivity activity) {
        netProtocol.getStore(activity, false, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                mStoreModelList.clear();
                mStoreModelList.addAll((List<StoreModel>) baseBean.getData());
            }

            @Override
            public void onFail(BaseBean baseBean) {
                Toast.makeText(activity, baseBean.getMessage(), Toast.LENGTH_SHORT);
            }
        });
    }

    public interface ICallbackStoreListener {
        void onSure(String storeId, String storeName);
    }
}
